package com.axsoft.jobimp;

import com.axsoft.jobimp.types.BLK;
import com.axsoft.jobimp.types.LongWord;
import com.axsoft.jobimp.types.OByte;
import com.axsoft.jobimp.types.Word;

/**
 * 
 * @author RankoR
 *
 * BEX Header
 * 
 * Byte: every server/client command starts from byte 0x23 - "#";
 * LongWord: sequence, from 0 to 0xFFFFFFFF, first packet has to start always from 0;
 * Word: BEX type;
 * Word: BEX subtype;
 * LongWord: request id, used for marking client requests to link it with server replies;
 * LongWord: following data length;
 * 
 */
public class OHeader {

	public static final byte HEADER_MARK = 0x23;
	public static final int HEADER_LENGTH = 17;
	public static final int DATA_OFFSET = HEADER_LENGTH;
	
	public OByte mMark;
	public LongWord mSequence;
	public Word mType;
	public Word mSubType;
	public LongWord mRequestId;
	public LongWord mLength;
	
	public OHeader() {
		mMark = new OByte(HEADER_MARK);
		mSequence = new LongWord();
		mType = new Word();
		mSubType = new Word();
		mRequestId = new LongWord();
		mLength = new LongWord();
	}
	
	public OHeader(final LongWord pSequence, final Word pType, final Word pSubType, final LongWord pRequestId, final LongWord pLength) {
		mMark = new OByte(HEADER_MARK);
		mSequence = pSequence;
		mType = pType;
		mSubType = pSubType;
		mRequestId = pRequestId;
		mLength = pLength;
	}
	
	public OHeader(final int pSequence, final short pType, final short pSubType, final int pRequestId, final int pLength) {
		mMark = new OByte(HEADER_MARK);
		mSequence = new LongWord(pSequence);
		mType = new Word(pType);
		mSubType = new Word(pSubType);
		mRequestId = new LongWord(pRequestId);
		mLength = new LongWord(pLength);
	}
	
	
	/**
	 * Parses Header contents from BLK
	 */
	public OHeader(final BLK pBLK) {
		final byte markByte = pBLK.getByte(0);
		if (markByte != Constants.OBIMP_BEX_ID) {
			return; // TODO: Throw Exception here!
		}
		
		
		
		
	}
	
	
	public final BLK toBLK() {
		final BLK blk = new BLK();
		
		blk.addBytes(mMark.getData());
		blk.addBytes(mSequence.getData());
		blk.addBytes(mType.getData());
		blk.addBytes(mSubType.getData());
		blk.addBytes(mRequestId.getData());
		blk.addBytes(mLength.getData());
		
		return blk;
	}
	
}
